package com.java.framework.utils;

import com.java.framework.utils.Criterion.Logical;
import com.java.framework.utils.Criterion.Operator;
import org.hibernate.criterion.MatchMode;

import java.util.Collection;

/**
 * 条件构造器 
 * 用于创建条件表达式
 * @author sunshine
 *
 */
public class Restrictions {

	/** 
     * 等于
     * @param fieldName
     * @param value
     * @return
     */
    public static <T> SimpleExpression<T> eq(String fieldName, Object value) {
    	return new SimpleExpression<T> (fieldName, value, Operator.EQ);
    }
    
    /** 
     * 不等于 
     * @param fieldName 
     * @param value 
     * @return 
     */  
    public static <T> SimpleExpression<T> ne(String fieldName, Object value) {  
        return new SimpleExpression<T> (fieldName, value, Operator.NE);  
    }  
  
    /** 
     * 模糊匹配 
     * @param fieldName 
     * @param value 
     * @return 
     */  
    public static <T> SimpleExpression<T> like(String fieldName, String value) {
        return new SimpleExpression<T> (fieldName, MatchMode.ANYWHERE.toMatchString(value), Operator.LIKE); 
    }
  
    /** 
     *  
     * @param fieldName 
     * @param value 
     * @param matchMode 
     * @return 
     */  
    public static <T> SimpleExpression<T> like(String fieldName, String value, MatchMode matchMode) {
    	switch(matchMode){
    	case START:
    		return new SimpleExpression<T> (fieldName, MatchMode.START.toMatchString(value), Operator.LIKE);
    	case END:
    		return new SimpleExpression<T> (fieldName, MatchMode.END.toMatchString(value), Operator.LIKE);
    	case ANYWHERE:
    		return new SimpleExpression<T> (fieldName, MatchMode.ANYWHERE.toMatchString(value), Operator.LIKE);
		default:
			return new SimpleExpression<T> (fieldName, MatchMode.ANYWHERE.toMatchString(value), Operator.LIKE);
    	}
    }  
  
    /** 
     * 大于 
     * @param fieldName 
     * @param value 
     * @return 
     */  
    public static <T> SimpleExpression<T> gt(String fieldName, Object value) {  
        return new SimpleExpression<T> (fieldName, value, Operator.GT);  
    }  
  
    /**
     * 小于
     * @param fieldName
     * @param value
     * @return
     */
    public static <T> SimpleExpression<T> lt(String fieldName, Object value) {
        return new SimpleExpression<T> (fieldName, value, Operator.LT);
    }
  
    /** 
     * 大于等于 
     * @param fieldName 
     * @param value 
     * @return 
     */  
    public static <T> SimpleExpression<T> lte(String fieldName, Object value) {
        return new SimpleExpression<T> (fieldName, value, Operator.LE);
    }  
  
    /** 
     * 小于等于 
     * @param fieldName 
     * @param value 
     * @return 
     */  
    public static <T> SimpleExpression<T> gte(String fieldName, Object value) {
        return new SimpleExpression<T> (fieldName, value, Operator.GE);
    }
    
    /** 
     * 包含于 
     * @param fieldName 
     * @param value 
     * @return 
     */
	public static<T> SimpleExpression<T> in(String fieldName, Collection<?> value, boolean ignoreNull) {
        if(ignoreNull&&(value==null||value.isEmpty())){
            return null;
        }
        return new SimpleExpression<T>(fieldName, value, Operator.IN);
    }
	
	public static<T> SimpleExpression<T> in(String fieldName, Collection<?> value) {        
        return in(fieldName, value, false);
    }
    
    /**
     * 不包含
     * @param fieldName
     * @param value
     * @param ignoreNull
     * @return
     */
	public static <T> SimpleExpression<T> notIn(String fieldName, Collection<?> value, boolean ignoreNull) {
    	if(ignoreNull&&(value==null||value.isEmpty())){
            return null;
        }
    	return new SimpleExpression<T>(fieldName, value, Operator.NIN);
    }
	
	public static <T> SimpleExpression<T> notIn(String fieldName, Collection<?> value) {    	
    	return notIn(fieldName, value, false);
    }
	
	/** 
     * 并且 
     * @param criterions 
     * @return 
     */
	@SafeVarargs
	public static <T> LogicalExpression<T> and(Criterion<T>... criterions){
        return new LogicalExpression<T>(criterions, Logical.AND);
    }
    
    /** 
     * 或者 
     * @param criterions 
     * @return 
     */  
    @SafeVarargs
	public static <T> LogicalExpression<T> or(Criterion<T>... criterions){
        return new LogicalExpression<T>(criterions, Logical.OR);
    }
}
